package org.dallasrowing.model.dao.sqlmap;

import java.sql.SQLException;
import java.util.List;

import org.dallasrowing.model.DamageEntry;
import org.dallasrowing.model.dao.DAOException;
import org.dallasrowing.model.dao.DamagesDAO;

import com.ibatis.sqlmap.client.SqlMapClient;

public class SqlMapDamagesDAO implements DamagesDAO {

	private static final String DAMAGES_ALL = "Damages.all";
	private static final String CREATE = "Damages.create";
	private static final String CLOSE_ENTRY = "Damages.close";
	private SqlMapClient sqlMapClient;

	public SqlMapDamagesDAO(SqlMapClient sqlMapClient )
	{
		this.sqlMapClient = sqlMapClient;
	}
	
	public void addDamageEntry(DamageEntry entry) throws DAOException {
		try {
			sqlMapClient.insert(CREATE, entry);
		} catch (SQLException e) {
			throw new DAOException("could not create a new entry in Damages", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<DamageEntry> allDamages() throws DAOException {
		try {
			return sqlMapClient.queryForList(DAMAGES_ALL);
		} catch (SQLException e) {
			throw new DAOException("could not query for all damages", e);
		}
	}

	public void closeDamageEntry(DamageEntry d) throws DAOException {
		try {
			sqlMapClient.update(CLOSE_ENTRY, d);
		} catch (SQLException e) {
			throw new DAOException("could not update closeDate timestamp", e);
		}
	}

}
